<template>
  <!-- wrap 包裹的意思 -->
  <div class="login-wrap">
    <!-- 展示一个登录的表单 -->
    <h2>公司内部管理平台</h2>
    <!-- ref 属性，代表对当前 dom 元素的一个引用 -->
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="70px"
      class="login-form"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          type="text"
          v-model="ruleForm.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { validUsername, validPassword } from "@/util/validate.js";
import { mapState } from "vuex";
let sessionstorage = require("sessionstorage");
export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error("用户名不能为空"));
      } else {
        if (!validUsername(value)) {
          callback(new Error("请输入正确的用户名"));
        }
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error("密码不能为空"));
      } else {
        if (!validPassword(value)) {
          callback(new Error("请输入正确的密码"));
        }
        callback();
      }
    };
    return {
      /* 用于接收表单的数据 */
      ruleForm: {
        username: "",
        password: "",
      },
      /* 用于定义表单验证 */
      rules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
    };
  },
  computed: {
    ...mapState(["tokenKey", "tokenValue", "department"]),
  },
  methods: {
    /* 表单提交 */
    submitForm() {
      /* 如果验证通过的时候，完成提交 */
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          /* 表单验证成功（调用登录接口，获取 token 凭证，然后在本地存储） */
          // alert("submit!");
          this.$message({
            message: "登录成功！",
            type: "success",
          });
          /* 在localStorage里面设置isLogin的值 */
          sessionstorage.setItem(this.tokenKey, this.tokenValue);
          /* 前往后台的首页 */
          this.$router.push("/admin"); // 前往后台的首页
        } else {
          // console.log("error submit!!");
          /* 验证失败 提示 */
          this.$message.error("用户名或密码不正确！");
          return false;
        }
      });
    },
    /* 表单重置 */
    resetForm() {
      this.$refs["ruleForm"].resetFields();
    },
  },
};
</script>

/* 完成登录页面的布局 */
<style scoped lang="less">
/* 让这个元素在父容器里面水平垂直居中，当前元素 flex-item，开启父容器 flex 布局 */
.login-wrap {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  justify-content: center;
  align-items: center;
  background: url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Foffice365.xin%2Fapp%2Fhome%2Fview%2Fdefault%2Fpublic%2Fimages%2Flogin.png&refer=http%3A%2F%2Foffice365.xin&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636598941&t=ab9e9bf9df992e3a1e07a8a513d4853c")
    no-repeat center;
  h2 {
    padding: 26px;
    color: #333;
    font-weight: normal;
    text-shadow: 1px 1px rgba(0, 0, 0, 0.5);
  }
  .login-form {
    width: 400px;
    height: 200px;
    padding: 10px;
    box-shadow: 1px 1px 6px 2px rgba(0, 0, 0, 0.3);
  }
}
</style>